package org.jeecg.modules.device.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.jeecg.common.config.TenantContext;
import org.jeecg.common.constant.DeviceConstant;
import org.jeecg.common.constant.TenantConstant;
import org.jeecg.common.system.api.ISysBaseAPI;
import org.jeecg.common.util.RedisUtil;
import org.jeecg.modules.device.entity.DevDeviceInfo;
import org.jeecg.modules.device.entity.DevDeviceRecord;
import org.jeecg.modules.device.entity.DevReservoirInfo;
import org.jeecg.modules.device.mapper.DevDeviceInfoMapper;
import org.jeecg.modules.device.mapper.DevDeviceRecordMapper;
import org.jeecg.modules.device.mapper.DevReservoirInfoMapper;
import org.jeecg.modules.device.service.IDevDeviceInfoService;
import org.jeecg.modules.drive.service.DriveCommonDbService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * @Description: 设备信息表
 * @Author: jeecg-boot
 * @Date:   2024-04-17
 * @Version: V1.0
 */
@Service
public class DevDeviceInfoServiceImpl extends ServiceImpl<DevDeviceInfoMapper, DevDeviceInfo> implements IDevDeviceInfoService {
    @Autowired
    private DevDeviceInfoMapper devDeviceInfoMapper;
    @Autowired
    private DriveCommonDbService driveCommonService;
    @Autowired
    private DevDeviceRecordMapper deviceRecordMapper;
    @Autowired
    private DevReservoirInfoMapper devReservoirInfoMapper;
    @Autowired
    private RedisUtil redisUtil;
    @Autowired
    private ISysBaseAPI sysBaseAPI;


    @Override
    public DevDeviceInfo queryByCod(String deviceCode) {
        return devDeviceInfoMapper.queryByCod(deviceCode);
        // return devDeviceInfoMapper.selectOne(Wrappers.<DevDeviceInfo>lambdaQuery()
        //         .eq(DevDeviceInfo::getDeviceCode, deviceCode).last("limit 1"));

    }

    /**
     * 根据监测点（水库ID）获取水库下所有设备
     * @param reservoirId
     */
    @Override
    public List<DevDeviceInfo> getDeviceListByReservoirId(Long reservoirId){
        return devDeviceInfoMapper.selectList(Wrappers.<DevDeviceInfo>lambdaQuery()
                .eq(DevDeviceInfo::getReservoirId, reservoirId)
                .eq(DevDeviceInfo::getDelFlag, 0));
    }

    /***
     * 同步所有监测点设备远程数据
     */
    @Override
    public void synRemoteData(){
        //缓存获取租户ID列表
        Set<Object> tenantSet = redisUtil.sGet(TenantConstant.SYS_TENANT_CACHE_SET);
        if(null != tenantSet && tenantSet.size()>0){
            for (Object tenantL : tenantSet) {
                TenantContext.setTenant(tenantL.toString());
                List<DevReservoirInfo> infoList = devReservoirInfoMapper.selectList(null);
                for (DevReservoirInfo info : infoList) {
                    this.readMonitorPointDeviceRemoteData(info.getId());
                }
            }
        }

        if(null == tenantSet || tenantSet.size()  == 0){
            List<DevReservoirInfo> infoList = devReservoirInfoMapper.selectList(null);
            for (DevReservoirInfo info : infoList) {
                this.readMonitorPointDeviceRemoteData(info.getId());
            }
        }
    }


    /**
     *  读取监测点设备远程数据
     *
     *  1.读取设备远程数据
     *  2.保存设备数据
     *  3.缓存设备数据-同一设备仅保留一条（覆盖）
     *
     * @param reservoirId 监测点（水库ID）
     */
    @Override
    public void readMonitorPointDeviceRemoteData(Long reservoirId){
        /**1.读取设备远程数据*/
        List<Map<String, Object>> deviceDataLs = driveCommonService.readReservoirData(reservoirId);
        if(null != deviceDataLs && deviceDataLs.size()>0){
            for (Map<String, Object> map : deviceDataLs) {
                String deviceType = map.get("deviceType").toString();
                String driveReadData =map.get("driveReadData").toString();
                String deviceUnit = (String) map.get("deviceUnit");
                String uniqueId = (String) map.get("uniqueId");
                DevDeviceRecord deviceRecord = new DevDeviceRecord();
                deviceRecord.setDeviceType(deviceType);
                deviceRecord.setDeviceCode(uniqueId);
                deviceRecord.setDeviceValue(driveReadData);
                deviceRecord.setDeviceUnit(deviceUnit);
                /**2.保存设备数据入库*/
                deviceRecordMapper.insert(deviceRecord);
                /**3.缓存设备数据*/
                redisUtil.hset(DeviceConstant.DEVICE_REALTIME_DATA,uniqueId,map);
            }
        }
    }
}
